//
//  PaperResultController.m
//  DoingPaper_LS
//
//  Created by zhuxuhong on 2016/10/26.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperResultController.h"
#import <Masonry/Masonry.h>


#pragma mark ProgressInfoView
@interface ProgressInfoView : UIView

-(instancetype)initWithIconName:(NSString *)icon
              progressTintColor: (UIColor*)tintColor;

-(instancetype)initWithIconName:(NSString *)icon
                       value:(NSInteger)value
                       total:(NSInteger)total;

-(void)setProgressValue:(NSInteger)value
                  total: (NSInteger)total;

@end

@interface ProgressInfoView()

@property(nonatomic,retain)UIImageView *iconView;
@property(nonatomic,retain)UIProgressView *progressView;
@property(nonatomic,retain)UILabel *infoLabel;

@end

@implementation ProgressInfoView

-(instancetype)initWithIconName:(NSString *)icon
              progressTintColor:(UIColor *)tintColor{
    if (self = [super initWithFrame:CGRectZero]) {
        [self setupUI];
        
        _iconView.image = [UIImage imageNamed:icon];
        [_progressView setProgressTintColor:tintColor];
    }
    return self;
}

-(instancetype)initWithIconName:(NSString *)icon
                       value:(NSInteger)value
                       total:(NSInteger)total {
    if (self = [super initWithFrame:CGRectZero]) {
        [self setupUI];
        
        _infoLabel.text = [NSString stringWithFormat:@"%ld",total];
    }
    return self;
}

-(void)setupUI{
    [self addSubview:self.iconView];
    [self addSubview:self.progressView];
    [self addSubview:self.infoLabel];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.width.height.equalTo(@(20));
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@(0));
        make.width.equalTo(@(50));
    }];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.equalTo(@(2));
        make.left.mas_equalTo(_iconView.mas_right).offset(20);
        make.right.mas_equalTo(_infoLabel.mas_left).offset(-20);
    }];
}

// 设置进度
-(void)setProgressValue:(NSInteger)value total: (NSInteger)total{
    _progressView.progress = value*1.0/total;
    _infoLabel.text = [NSString stringWithFormat:@"%ld",value];
}

-(UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [UIImageView new];
    }
    return _iconView;
}

-(UIProgressView *)progressView{
    if (!_progressView) {
        _progressView = [UIProgressView new];
    }
    return _progressView;
}

-(UILabel *)infoLabel{
    if (!_infoLabel) {
        _infoLabel = [UILabel new];
    }
    return _infoLabel;
}


@end


#pragma mark PaperResultController
@interface PaperResultController ()

@property(nonatomic,retain)UIImageView *iconView;
@property(nonatomic,retain)UILabel *iconLabel;
@property(nonatomic,retain)UILabel *resultInfoLabel;
@property(nonatomic,retain)ProgressInfoView *rightProgressInfoView;
@property(nonatomic,retain)ProgressInfoView *wrongProgressInfoView;
@property(nonatomic,retain)UITableView *analysisTableView;
@property(nonatomic,retain)UIView *containerView;
@property(nonatomic,retain)UIButton *scanAnswerBtn;

@end

@implementation PaperResultController
{
    UIColor *mainColor;
    UIColor *redColor;
}

-(instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)setPaper:(PaperModel *)paper{
    if (paper) {
        NSInteger total = paper.rightCount + paper.wrongCount;
        NSInteger score = paper.gotScore;
        _resultInfoLabel.text = [NSString stringWithFormat:@"总分: %@  得分: %@  用时: %@",paper.score, @(score), paper.usedTime];
        [_rightProgressInfoView setProgressValue: paper.rightCount total:total];
        [_wrongProgressInfoView setProgressValue: paper.wrongCount total:total];
        if (score < total*0.6) { // >=60分及格
            _iconView.image = [UIImage imageNamed:@"icon_face_cry"];
            _iconLabel.text = @"小伙子，你得加把劲啊！";
            _iconLabel.textColor = redColor;
        }
        _paper = paper;
    }
}

-(void)setupUI{
    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
    mainColor = [UIColor colorWithRed:36/255.0 green:154/255.0 blue:131/255.0 alpha:1];
    redColor = [UIColor colorWithRed:253/255.0 green:105/255.0 blue:106/255.0 alpha:1];
    
    [self.view addSubview:self.scanAnswerBtn];
    [self.view addSubview:self.containerView];
    [_containerView addSubview:self.iconView];
    [_containerView addSubview:self.iconLabel];
    [_containerView addSubview:self.resultInfoLabel];
    [_containerView addSubview:self.rightProgressInfoView];
    [_containerView addSubview:self.wrongProgressInfoView];
    [_containerView addSubview:self.analysisTableView];
    
    [self setupLayout];
}

-(void)setupLayout{
    UILabel *tips = [self labelWithDefaultText:@"薄弱分析" textColor:mainColor fontSize:20];
    [_containerView addSubview:tips];

    CGFloat space40 = 40;
    CGFloat space10 = 20;
    [_scanAnswerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(space10));
        make.bottom.right.equalTo(@(-space10));
        make.height.equalTo(@(space40));
    }];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(space10));
        make.right.equalTo(@(-space10));
        make.bottom.mas_equalTo(_scanAnswerBtn.mas_top).offset(-space10);
    }];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(space40));
        make.width.height.equalTo(@(60));
    }];
    [_iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_iconView.mas_right).offset(space10);
        make.right.equalTo(@(-space40));
        make.centerY.mas_equalTo(_iconView.mas_centerY);
    }];
    [_resultInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(space40));
        make.right.equalTo(@(-space40));
        make.top.mas_equalTo(_iconView.mas_bottom).offset(space10);
    }];
    [_rightProgressInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_resultInfoLabel);
        make.top.mas_equalTo(_resultInfoLabel.mas_bottom).offset(space10);
        make.height.equalTo(@(space40));
    }];
    [_wrongProgressInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(_rightProgressInfoView);
        make.top.mas_equalTo(_rightProgressInfoView.mas_bottom);
    }];
    [tips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_wrongProgressInfoView);
        make.top.mas_equalTo(_wrongProgressInfoView.mas_bottom).offset(space10);
    }];
    [_analysisTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_wrongProgressInfoView);
        make.top.mas_equalTo(tips.mas_bottom).offset(space10);
        make.bottom.equalTo(@(-space40));
    }];
}

#pragma mark - tools

-(UILabel*)labelWithDefaultText: (NSString*)text
                      textColor: (UIColor*)color
                       fontSize: (CGFloat)size{
    
    UILabel *label = [UILabel new];
    label.text = text;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:size];
    
    return label;
}

#pragma mark - action
-(void)dismissToScanAnswer: (UIButton*)sender{
    [self dismissViewControllerAnimated:true completion:^{
        if (_scanAnswerBlock) {
            _scanAnswerBlock();
        }
    }];
}


#pragma mark - getters
// 查看答案
-(UIButton *)scanAnswerBtn{
    if (!_scanAnswerBtn) {
        _scanAnswerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _scanAnswerBtn.backgroundColor = mainColor;
        [_scanAnswerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_scanAnswerBtn setTitle:@"查看答案" forState:UIControlStateNormal];
        [_scanAnswerBtn addTarget:self action:@selector(dismissToScanAnswer:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanAnswerBtn;
}

// 包裹
-(UIView*)containerView{
    if (!_containerView) {
        UIView *v = [UIView new];
        v.backgroundColor = [UIColor whiteColor];
        v.layer.borderColor = [UIColor lightGrayColor].CGColor;
        v.layer.borderWidth = 1.0;
        v.layer.cornerRadius = 4.0;
        _containerView = v;
    }
    return _containerView;
}


// 表情
-(UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [UIImageView new];
        _iconView.image = [UIImage imageNamed:@"icon_face_smile"];
    }
    return _iconView;
}

// 提示
-(UILabel *)iconLabel{
    if (!_iconLabel) {
        _iconLabel = [self labelWithDefaultText:@"哎呦不错哦，小伙子继续加油💪！" textColor:mainColor fontSize:30];
    }
    return _iconLabel;
}

// 总分：得分：用时
-(UILabel *)resultInfoLabel{
    
    if (!_resultInfoLabel) {
        _resultInfoLabel = [self labelWithDefaultText:@"总分: 得分: 用时: " textColor:[UIColor blackColor] fontSize:20];
    }
    return _resultInfoLabel;
}

// 对
-(ProgressInfoView *)rightProgressInfoView{
    if (!_rightProgressInfoView) {
        _rightProgressInfoView = [[ProgressInfoView alloc] initWithIconName:@"icon_right" progressTintColor:mainColor];
    }
    return _rightProgressInfoView;
}

// 错
-(ProgressInfoView *)wrongProgressInfoView{
    if (!_wrongProgressInfoView) {
        _wrongProgressInfoView = [[ProgressInfoView alloc] initWithIconName:@"icon_wrong" progressTintColor:redColor];
    }
    return _wrongProgressInfoView;
}

// 分析table
-(UITableView *)analysisTableView{
    if (!_analysisTableView) {
        _analysisTableView = [UITableView new];
    }
    return _analysisTableView;
}
@end
